package com.briup.auth.web.controller;

import com.briup.auth.service.SmsVerifyCodeService;
import com.briup.auth.service.UserAuthService;
import com.briup.common.web.Message;
import com.briup.common.web.MessageUtil;
import com.briup.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author wood
 */
@Api(tags = "用户认证模块")
@RestController
public class AuthController {

    @Autowired
    private UserAuthService userAuthService;

    @Autowired
    private SmsVerifyCodeService smsVerifyCodeService;

    @ApiOperation(value = "用户名登录",notes = "登录后获取token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,defaultValue = "tom"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,defaultValue = "123"),
    })
    @PostMapping("/login/username")
    public Message loginWithUsername(String username, String password) {
        String jwtToken = userAuthService.loginWithUsernameAndPassword(username,password);
        return MessageUtil.success(jwtToken);
    }


    @ApiOperation(value = "手机号登录",notes = "登录后获取token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "telephone",value = "手机号",required = true,defaultValue = "13812811111"),
            @ApiImplicitParam(name = "verifyCode",value = "验证码",required = true,defaultValue = "briup"),
    })
    @PostMapping("/login/telephone")
    public Message loginWithTelephone(String telephone, String verifyCode) {
        String jwtToken = userAuthService.loginWithTelephoneAndVerifyCode(telephone,verifyCode);
        return MessageUtil.success(jwtToken);
    }

    @ApiOperation(value = "获取手机验证码",notes = "手机号登录前，需要先获取手机验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "telephone",value = "手机号",required = true),
    })
    @GetMapping("/login/telephone/verifyCode")
    public Message getVerifyCode(String telephone) {

        if(smsVerifyCodeService.sendCode(telephone)){
            return MessageUtil.success("短信验证码已经发送");
        }

        return MessageUtil.error("短信验证码发送失败");
    }

    @ApiOperation(value = "获取登录用户信息",notes = "获取登录用户信息")
    @GetMapping("/me")
    public Message me(@ApiIgnore @RequestHeader("Authorization") String jwtToken) {
        User user = userAuthService.getUserFromToken(jwtToken);
        return MessageUtil.success(user);
    }

    @ApiOperation(value = "验证token是否合法",notes = "验证token是否合法")
    @PostMapping("/check/token")
    public Message checkToken(@ApiIgnore @RequestHeader("Authorization") String jwtToken) {
        boolean result = userAuthService.checkToken(jwtToken);
        return MessageUtil.success(result);
    }

    @ApiOperation(value = "用户退出",notes = "用户退出")
    @GetMapping("/logout")
    public Message logout(@ApiIgnore @RequestHeader("Authorization") String jwtToken) {
        userAuthService.logout(jwtToken);
        return MessageUtil.success("退出成功");
    }

}
